package cn.yx.common.knife4j.configuration;

import cn.yx.common.knife4j.properties.Knife4jProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * <p>Knife4j配置类，实际为Swagger的基础配置</p>
 *
 * @author Wgssmart
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(Knife4jProperties.class)
public class Knife4jConfiguration {

    private final Knife4jProperties knife4jProperties;

    public Knife4jConfiguration(Knife4jProperties knife4jProperties) {
        this.knife4jProperties = knife4jProperties;
    }

    @Bean
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title(knife4jProperties.getTitle())
                        .description(knife4jProperties.getDescription())
                        .termsOfServiceUrl(knife4jProperties.getTermsOfServiceUrl())
                        .contact(new Contact(knife4jProperties.getContact().getName(),
                                knife4jProperties.getContact().getUrl(), knife4jProperties.getContact().getEmail()))
                        .version(knife4jProperties.getVersion())
                        .build())
                // 分组名称
                .groupName(knife4jProperties.getGroupName())
                .select()
                // Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage(knife4jProperties.getBasePackage()))
                .paths(PathSelectors.any())
                .build();
    }
}